<template>
  <div class="app-container flex column">
    <top-title :updateTime="true" :content="content"></top-title>
    <div class="content-wrap f1">
      <!-- 搜索部分 -->
      <div class="search-group" style="padding-left: 0">
        <div class="input-wrap">
          <div class="label">用户账号:</div>
          <div class="input-item">
            <j-input :maxLength="30" placeholder="请输入用户账号模糊查询" v-model="queryParam.username"></j-input>
          </div>
        </div>
        <div class="input-wrap">
          <div class="label">用户名称:</div>
          <div class="input-item">
            <j-input :maxLength="30" placeholder="请输入用户姓名" v-model="queryParam.realname"></j-input>
          </div>
        </div>
        <div class="input-wrap">
          <div class="label">用户状态:</div>
          <div class="input-item">
            <a-select v-model="queryParam.status" placeholder="请选择用户状态">
              <a-select-option value="1">正常</a-select-option>
              <a-select-option value="2">冻结</a-select-option>
            </a-select>
          </div>
        </div>

        <div class="btn-wrap">
          <a-button class="searchBtn" @click="searchQuery()" type="primary">搜索</a-button>
          <a-button class="resetBtn" @click="searchReset()" type="primary">重置</a-button>
        </div>
      </div>

      <!-- 操作按钮区域 -->
      <div class="table-operator" style="border-top: 5px">
        <a-button @click="handleAdd" class="BtnStyle" type="primary" icon="plus">添加用户</a-button>
        <a-button type="primary" icon="download" class="BtnStyle" @click="handleExportXls('用户信息')">导出</a-button>
        <a-upload
          name="file"
          :showUploadList="false"
          :multiple="false"
          :headers="tokenHeader"
          accept=".xls,.xlsx,"
          :action="importExcelUrl"
          @change="handleImportExcel"
        >
          <a-button type="primary" class="BtnStyle" icon="import">导入</a-button>
        </a-upload>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay" @click="handleMenuClick">
            <a-menu-item key="1">
              <a-icon type="delete" @click="batchDel" />
              删除
            </a-menu-item>
            <a-menu-item key="2">
              <a-icon type="lock" @click="batchFrozen('2')" />
              冻结
            </a-menu-item>
            <a-menu-item key="3">
              <a-icon type="unlock" @click="batchFrozen('1')" />
              解冻
            </a-menu-item>
          </a-menu>
          <a-button class="BtnStyle" style="margin-left: 8px">
            批量操作
            <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <!-- table区域 -->
      <div class="table-wrap">
        <a-table
          rowKey="id"
          class="tableList"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="handleTableChange"
        >
          <template #pic="records">
            <img v-if="records.avatar" :src="imgPrefix + records.avatar" style="width: 0.5rem; height: 0.5rem" alt="" />
          </template>
          <template v-slot:status="record">
            <span>{{ record.status === 1 ? '正常' : '冻结' }}</span>
          </template>

          <span slot="action" slot-scope="text, record">
            <a class="operation dropdown-color" @click="handleEdit(record)">编辑</a>
            <!-- <a-divider type="vertical" /> -->
            <a-dropdown>
              <a class="ant-dropdown-link operation dropdown-color">
                更多
                <a-icon type="down" />
              </a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a href="javascript:;" @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a href="javascript:;" @click="handleChangePassword(record.username)">密码</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
                <a-menu-item v-if="record.status == 1">
                  <a-popconfirm title="确定冻结吗?" @confirm="() => handleFrozen(record.id, 2, record.username)">
                    <a>冻结</a>
                  </a-popconfirm>
                </a-menu-item>
                <a-menu-item v-if="record.status == 2">
                  <a-popconfirm title="确定解冻吗?" @confirm="() => handleFrozen(record.id, 1, record.username)">
                    <a>解冻</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
      </div>
    </div>
    <detail-modal ref="detailModal"></detail-modal>
    <user-modal ref="modalForm" @refresh="searchQuery()"></user-modal>
    <password-modal ref="passwordmodal" @refresh="searchQuery()"></password-modal>
  </div>
</template>

<script>
import { frozenBatch } from '@/api/api'
import UserModal from './modules/UserModal'
import topTitle from '@/components/topTitle'
import JInput from '@/components/jeecg/JInput'
import detailModal from './modules/detailModal'
import PasswordModal from './modules/PasswordModal'
import { comMethods } from '@/mixins/comMethods'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
export default {
  name: '',
  mixins: [JeecgListMixin, comMethods],
  components: { topTitle, JInput, UserModal, PasswordModal, detailModal },
  data() {
    return {
      name: 'UserList',
      content: [['用户管理']],
      url: {
        list: '/sys/user/page',
        delete: '/sys/user/delete',
        exportXlsUrl: '/sys/user/exportXls',
        deleteBatch: '/sys/user/deleteBatch',
        importExcelUrl: 'sys/user/importExcel',
      },
      imgPrefix: '',
      columns: [
        {
          title: '用户账号',
          ellipsis: true,
          align: 'center',
          dataIndex: 'username',
          sorter: true,
        },
        {
          title: '用户名称',
          ellipsis: true,
          align: 'center',
          dataIndex: 'realname',
        },

        {
          title: '所属机关',
          ellipsis: true,
          align: 'center',
          dataIndex: 'unitId_dictText',
        },
        {
          title: '用户状态',
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '操作',
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },
  computed: {
    importExcelUrl: function () {
      // return `http://192.168.1.77:8089/lampblack-boot/web/${this.url.importExcelUrl}`
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  mounted() {},
  methods: {
    handleMenuClick(e) {
      if (e.key == 1) {
        this.batchDel()
      } else if (e.key == 2) {
        this.batchFrozen(2)
      } else if (e.key == 3) {
        this.batchFrozen(1)
      }
    },

    batchFrozen: function (status) {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return false
      } else {
        let ids = ''
        let that = this
        let isAdmin = false
        that.selectionRows.forEach(function (row) {
          if (row.username == 'admin') {
            isAdmin = true
          }
        })
        if (isAdmin) {
          that.$message.warning('管理员账号不允许此操作,请重新选择！')
          return
        }
        that.selectedRowKeys.forEach(function (val) {
          ids += val + ','
        })
        that.$confirm({
          title: '确认操作',
          content: '是否' + (status == 1 ? '解冻' : '冻结') + '选中账号?',
          onOk: function () {
            frozenBatch({ ids: ids, status: status }).then((res) => {
              if (res.code == 200) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
    handleFrozen(id, status, username) {
      if ('admin' == username) {
        this.$message.warning('管理员账号不允许此操作！')
        return
      }
      frozenBatch({ ids: id, status: status }).then((res) => {
        if (res.code == 200) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleChangePassword(username) {
      this.$refs.passwordmodal.show(username)
    },
    handleDetail(record) {
      this.$refs.detailModal.show(record)
    },
  },
  destroy() {},
}
</script>

<style scoped lang="less">
/deep/ .ant-upload,
.ant-upload.ant-upload-select-picture-card {
  background: none !important;
  border-color: none !important;
}
</style>
